set more off
clear
cd "D:\Projects\HeterogeneousExternalities\Data\"

cap mkdir "D:\Projects\HeterogeneousExternalities\Output\"
use Daily_Data.dta,clear


gen lrbrent=ln(rbrent)


*2-week linear ramps
gen RVP9_ramp=RVP9
gen RVP78_ramp=RVP78
gen RVP7_ramp=RVP7
gen RFG_ramp=RFG


local ramp_length=15
sort station_fe date
forvalues i=1/`ramp_length'{
bysort station_fe: replace RVP9_ramp=1-`i'*(1/`ramp_length') if RVP9_on[_n+`i']==1
bysort station_fe: replace RVP9_ramp=1-`i'*(1/`ramp_length') if RVP9_off[_n-`i'+1]==1

bysort station_fe: replace RVP78_ramp=1-`i'*(1/`ramp_length') if RVP78_on[_n+`i']==1
bysort station_fe: replace RVP78_ramp=1-`i'*(1/`ramp_length') if RVP78_off[_n-`i'+1]==1

bysort station_fe: replace RVP7_ramp=1-`i'*(1/`ramp_length') if RVP7_on[_n+`i']==1
bysort station_fe: replace RVP7_ramp=1-`i'*(1/`ramp_length') if RVP7_off[_n-`i'+1]==1

bysort station_fe: replace RFG_ramp=1-`i'*(1/`ramp_length') if RFG_on[_n+`i']==1
bysort station_fe: replace RFG_ramp=1-`i'*(1/`ramp_length') if RFG_off[_n-`i'+1]==1

}

*First regression is just a placeholder
reghdfe lrgas, noabsorb
outreg2 using "D:\Projects\HeterogeneousExternalities\Output\Full Sample Stage 1.tex", replace keep(lrGDP   RVP9_ramp RVP78_ramp RVP7_ramp RFG_ramp hdd mean_precip snow cdd depth ) dec(3) addtext(F-stat of Excluded Instruments, `f' , Weather Controls, Yes, Sensor FE, Yes, DOW FE, No, MOY FE, No, County*DOW FE, Yes, County*MOY FE, Yes)


reghdfe  lrgas lrbrent hdd mean_precip snow cdd depth      , absorb(station_fe  county_DOW county_month) cluster(county_fe) 
outreg2 using "D:\Projects\HeterogeneousExternalities\Output\Full Sample Stage 1.tex", append keep( lrGDP lrbrent lrgas hdd mean_precip snow cdd depth ) dec(3) addtext(F-stat of Excluded Instruments, `f' , Weather Controls, Yes, Sensor FE, Yes, DOW FE, No, MOY FE, No, County*DOW FE, Yes, County*MOY FE, Yes)



reghdfe lrgas  hdd mean_precip snow cdd depth  lrGDP RVP9_ramp RVP78_ramp RVP7_ramp RFG_ramp   , absorb(station_fe  DOW  MOY) cluster(county_fe) 
outreg2 using "D:\Projects\HeterogeneousExternalities\Output\Full Sample Stage 1.tex", append keep(lrGDP   RVP9_ramp RVP78_ramp RVP7_ramp RFG_ramp hdd mean_precip snow cdd depth ) dec(3) addtext(F-stat of Excluded Instruments, `f' , Weather Controls, Yes, Sensor FE, Yes, DOW FE, Yes, MOY FE, Yes, County*DOW FE, No, County*MOY FE, No)



reghdfe lrgas  hdd mean_precip snow cdd depth lrGDP RVP9_ramp RVP78_ramp RVP7_ramp RFG_ramp  , absorb(station_fe  county_DOW  county_MOY) cluster(county_fe) 
outreg2 using "D:\Projects\HeterogeneousExternalities\Output\Full Sample Stage 1.tex", append keep(lrGDP   RVP9_ramp RVP78_ramp RVP7_ramp RFG_ramp hdd mean_precip snow cdd depth ) dec(3) addtext(F-stat of Excluded Instruments, `f' , Weather Controls, Yes, Sensor FE, No, DOW FE, No, MOY FE, No, County*DOW FE, Yes, County*MOY FE, Yes)





reghdfe lveh lrgas hdd mean_precip snow cdd depth  lrGDP    , absorb(station_fe   county_DOW county_MOY) cluster(county_fe) 
outreg2 using "D:\Projects\HeterogeneousExternalities\Output\Full Sample Stage 2.tex", replace keep( lrGDP lrgas hdd mean_precip snow cdd depth ) dec(3) addtext(F-stat of Excluded Instruments, `f' , Weather Controls, Yes, Sensor FE, Yes, DOW FE, Yes, MOY FE, Yes, County*DOW FE, Yes, County*MOY FE, Yes)



ivreghdfe lveh hdd mean_precip snow cdd depth    (lrgas=lrbrent)  , absorb(station_fe   county_DOW county_month) cluster(county_fe) 
local f=round(e(widstat),.001)

outreg2 using "D:\Projects\HeterogeneousExternalities\Output\Full Sample Stage 2.tex", append keep( lrGDP lrbrent  lrgas hdd mean_precip snow cdd depth ) dec(3) addtext(F-stat of Excluded Instruments, `f' , Weather Controls, Yes, Sensor FE, Yes, DOW FE, No, MOY FE, No, County*DOW FE, Yes, County*MOY FE, No, County*Month FE, Yes)



ivreghdfe lveh hdd mean_precip snow cdd depth lrGDP  (lrgas=RVP9_ramp RVP78_ramp RVP7_ramp RFG_ramp)  , absorb(station_fe  DOW MOY) cluster(county_fe)
local f=round(e(widstat),.001)
outreg2 using "D:\Projects\HeterogeneousExternalities\Output\Full Sample Stage 2.tex", append keep( lrGDP lrgas hdd mean_precip snow cdd depth ) dec(3) addtext(F-stat of Excluded Instruments, `f' , Weather Controls, Yes, Sensor FE, Yes, DOW FE, Yes, MOY FE, Yes, County*DOW FE, No, County*MOY FE, No, County*Month FE, No)



ivreghdfe lveh hdd mean_precip snow cdd depth  lrGDP  (lrgas=RVP9_ramp RVP78_ramp RVP7_ramp RFG_ramp)  , absorb(station_fe   county_DOW county_MOY) cluster(county_fe) 
local f=round(e(widstat),.001)
outreg2 using "D:\Projects\HeterogeneousExternalities\Output\Full Sample Stage 2.tex", append keep( lrGDP  lrgas hdd mean_precip snow cdd depth ) dec(3) addtext(F-stat of Excluded Instruments, `f' , Weather Controls, Yes, Sensor FE, No, DOW FE, No, MOY FE, No, County*DOW FE, Yes, County*MOY FE, Yes, County*Month FE, No)









